package com.guoyan.blockchain.common.config;

import com.guoyan.blockchain.common.client.FabricClient;
import com.guoyan.blockchain.common.user.UserContext;
import com.guoyan.blockchain.common.util.Util;
import org.hyperledger.fabric.sdk.Enrollment;
import org.hyperledger.fabric.sdk.NetworkConfig;
import org.hyperledger.fabric.sdk.User;
import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.NetworkConfigurationException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 * @author yeym
 */
@Configuration
public class BeanMgr {
//
//    @Bean(name = "org1Admin")
//    public UserContext getAdminOrg1() {
//        File file = new File("");
//        System.out.println(file.getAbsolutePath());
//        UserContext org1Admin = new UserContext();
//        String key = "../network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp";
//        File pkFolder1 = new File(key + "/keystore");
//        File[] pkFiles1 = pkFolder1.listFiles();
//        File certFolder1 = new File(key + "/signcerts");
//        File[] certFiles1 = certFolder1.listFiles();
//        Enrollment enrollOrg1Admin = null;
//        try {
//            enrollOrg1Admin = Util.getEnrollment(key + "/keystore", pkFiles1[0].getName(), key + "/signcerts", certFiles1[0].getName());
//        } catch (IOException | InvalidKeySpecException | NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        }
//        org1Admin.setEnrollment(enrollOrg1Admin);
//        org1Admin.setMspId("Org1MSP");
//        org1Admin.setName("admin");
//        return org1Admin;
//    }
//
//    @Bean(name = "org2Admin")
//    public UserContext getAdminOrg2() {
//        UserContext org2Admin = new UserContext();
//        String key = "../network/crypto-config/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp";
//        File pkFolder2 = new File(key + "/keystore");
//        File[] pkFiles2 = pkFolder2.listFiles();
//        File certFolder2 = new File(key + "/signcerts");
//        File[] certFiles2 = certFolder2.listFiles();
//        Enrollment enrollOrg2Admin = null;
//        try {
//            enrollOrg2Admin = Util.getEnrollment(key + "/keystore", pkFiles2[0].getName(), key + "/signcerts", certFiles2[0].getName());
//        } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) {
//            e.printStackTrace();
//        }
//        org2Admin.setEnrollment(enrollOrg2Admin);
//        org2Admin.setMspId("Org2MSP");
//        org2Admin.setName("admin");
//        return org2Admin;
//    }
//
//    @Bean(name = "ordererAdmin")
//    public UserContext getOrdererAdmin() {
//        UserContext org2Admin = new UserContext();
//        String key = "../network/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp";
//        File pkFolder2 = new File(key + "/keystore");
//        File[] pkFiles2 = pkFolder2.listFiles();
//        File certFolder2 = new File(key + "/admincerts");
//        File[] certFiles2 = certFolder2.listFiles();
//        Enrollment enrollOrg2Admin = null;
//        try {
//            enrollOrg2Admin = Util.getEnrollment(key + "/keystore", pkFiles2[0].getName(), key + "/admincerts", certFiles2[0].getName());
//        } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) {
//            e.printStackTrace();
//        }
//        org2Admin.setEnrollment(enrollOrg2Admin);
//        org2Admin.setMspId("OrdererMSP");
//        org2Admin.setName("admin");
//        return org2Admin;
//    }
    @Bean(name = "networkConfig")
    public NetworkConfig networkConfig(){
        try {
            return NetworkConfig.fromYamlFile(new File("../network/connection-Org1.yml"));
        } catch (InvalidArgumentException | IOException | NetworkConfigurationException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Bean(name = "org1Admin")
    public User getOrg1Admin(@Qualifier("networkConfig") NetworkConfig networkConfig) throws NetworkConfigurationException {
        return networkConfig.getPeerAdmin("Org1");
    }

    @Bean(name = "org2Admin")
    public User getOrg2Admin(@Qualifier("networkConfig") NetworkConfig networkConfig) throws NetworkConfigurationException {
        return networkConfig.getPeerAdmin("Org2");
    }

    @Bean(name = "org3Admin")
    public User getOrg3Admin(@Qualifier("networkConfig") NetworkConfig networkConfig) throws NetworkConfigurationException {
        return networkConfig.getPeerAdmin("Org3");
    }


    @Bean(name = "fabricClient")
    public FabricClient fabricClient(@Qualifier("networkConfig") NetworkConfig networkConfig) throws IllegalAccessException, InvocationTargetException, InvalidArgumentException, InstantiationException, NoSuchMethodException, CryptoException, ClassNotFoundException, NetworkConfigurationException {
        return new FabricClient(networkConfig.getPeerAdmin());
    }



}
